package com.henry.db.migrate.main;

import com.henry.db.migrate.task.BaseTable;
import com.henry.db.migrate.task.MigrateTask;
import com.henry.db.migrate.task.MigrateThread;
import com.henry.db.migrate.util.CommonUtil;
import com.henry.db.migrate.util.FileUtil;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MigrateClient {
    private static final Logger logger = Logger.getLogger(MigrateClient.class);
    public static void main(String[] args){
        logger.info("程序开始运行");
        ExecutorService threadPool = Executors.newFixedThreadPool(100);
        List<String> tables = FileUtil.readFile(CommonUtil.getRelativePath("tables.txt"));
        for (int i = 0; i < tables.size(); i++) {
            MigrateTask task= new MigrateTask(new BaseTable(tables.get(i)));
            ThreadGroup group = new ThreadGroup(task.getBaseTable().getTargetTable());
            for (int j = 0; j < task.getPageNum(); j++) {
                MigrateThread mythread = new MigrateThread(group,task.getBaseTable(),j);
                threadPool.execute(mythread);
            }
        }

        threadPool.shutdown();
        while (true) {
            if (threadPool.isTerminated()) {
                logger.info("全部子线程执行完毕");
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        logger.info("程序运行结束");
    }
}
